import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))
const Data = [{ name:'小于', value: '<' },{ name:'大于', value: '>' },{ name:'等于', value: '=' }]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '物料编号',			name: 'mate_number' },
			{ label: '物料名称',			name: 'mate_name' },
			{ label: '物料分类',			name: 'mate_type', type:'select', loading:true },
			{ label: '未使用量',			name:'condition', type:'select', data:Data },
			{ label: '',				name: 'unuse_qty', p:'请输入未使用量'},
			{ label: '最后采购时间',		names: ['start_date','end_date'], type:'date-range' },
		],
		// 提交表单列表
		formData : [
			{ label: '物料编号',		name: 'mate_number', 	full:true, required:true },
			{ label: '物料名称',		name: 'mate_name', 		half:true, required:true },
			{ label: '物料分类',		name: 'mate_type', 		half:true, type:'select', loading:true },
			{ label: '描述',			name: 'mate_content', 	full:true, type:'textarea' },
		]
	}
	
	model = {}
	
	componentDidMount(){
		
		this.getSelect()
		if(window.isInit){
			this.fetch()
		}
	}
	// 获取下拉数据
	getSelect = async () => {
		const { forms, formData } = this.state
		$cache.then( async f => {
			// 物料分类
			$fn.setRows([forms,formData], 'mate_type',  await f.getDisCache({ code: 26000}))
			//
			this.setState({ forms })
		})
		
	}
	// paging
	fetch = () => $fn.fetch.call(this,'material/index')
	
	// 表单
	cols = [
		{ title: '物料名称', 		dataIndex: 'mate_name', 		width:150 },
		{ title: '物料编号', 		dataIndex: 'mate_number', 		width:200 },
		{ title: '物料类型', 		dataIndex: 'mate_type_name', 	width:80 },
		{ title: '单位', 			dataIndex: 'unit_name', 		width:80 },
		{ title: '总量', 			dataIndex: 'total', 			...$fn.table.num },
		{ title: '未使用量', 		dataIndex: 'unuse_qty', 		...$fn.table.num },
		{ title: '使用量', 			dataIndex: 'use_qty', 			...$fn.table.num },
		{ title: '过期量', 			dataIndex: 'expire_qty', 		...$fn.table.num },
		{ title: '即将过期量', 		dataIndex: 'coming_soon_qty', 	...$fn.table.num },
		{ title: '保质期', 			dataIndex: 'shelf_life', 		...$fn.table.num },
		{ title: '最后采购时间', 		dataIndex: 'updated_at', 		...$fn.table.time },
		TableControls.controls.call(this)
	]
	// 表格操作按钮
	table_controls = (text, { uuid, box_status, type }) => {
		return [
			TableControls.edit.call(this, 'material/info', {uuid, box_status:+box_status, type:+type})
		]
	}
	
	render(){
		return (
			<>
				<Page title='物料列表'>
					{/* 搜索 */}
					{ SearchForm.call(this, { hasMany: false }) }
					{/* 表格 */}
					{ Table.call(this, { type: -1, isInit:window.isInit }) }
				</Page>
				{/* 编辑 */}
				{ AddEditModal.call(this,{ labelWidth:'s7', width:800, editApi: 'material/edit'}) }
			</>
		)
	}
}

export default Index